﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Xml;
using com.loto.Common;

namespace com.huigo.Framework.BLL
{
    public class Province
    {
        public static List<Model.Province> GetProvinceList()
        {
            List<Model.Province> provinceList = (List<Model.Province>)HttpContext.Current.Cache["ProvinceList"];
            if (provinceList != null && provinceList.Count > 0)
            {
                return provinceList;
            }
            try
            {
                provinceList = new List<Model.Province>();
                string configPath = PathHandle.GetFilePath(ConfigurationManager.AppSettings["PROVINCE"]);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configPath);
                XmlNodeList nodeList = xmlDoc.SelectNodes("//province");
                foreach (XmlNode node in nodeList)
                {
                    Model.Province province = new com.huigo.Framework.Model.Province();
                    province.ID = Convert.ToInt16(node.Attributes["ID"].Value);
                    province.Name = node.Attributes["Name"].Value;
                    province.Postage = Convert.ToDecimal(node.Attributes["Postage"].Value);
                    provinceList.Add(province);
                    //provinceList.Add(new Model.Province(Convert.ToInt16(node.Attributes["ID"].Value), node.Attributes["Name"].Value, Convert.ToDecimal(node.Attributes["Postage"].Value)));
                }

                CacheDependency cacheDependency = new CacheDependency(configPath);
                HttpContext.Current.Cache.Insert("ProvinceList", provinceList, cacheDependency);
                return provinceList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static List<Model.City> GetCityListByProvinceId(int provinceId)
        {
            List<Model.City> cityList = (List<Model.City>)HttpContext.Current.Cache["CityList_" + provinceId];
            if (cityList != null && cityList.Count > 0)
            {
                return cityList;
            }
            try
            {
                cityList = new List<Model.City>();
                string configPath = PathHandle.GetFilePath(ConfigurationManager.AppSettings["PROVINCE"]);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configPath);
                XmlNodeList nodeList = xmlDoc.SelectNodes("//province[@ID='" + provinceId + "']/city");
                foreach (XmlNode node in nodeList)
                {
                    Model.City city = new com.huigo.Framework.Model.City();
                    city.ID = Convert.ToInt16(node.Attributes["ID"].Value);
                    city.Name = node.Attributes["Name"].Value;
                    cityList.Add(city);
                    //cityList.Add(new Model.City(Convert.ToInt16(node.Attributes["ID"].Value), node.Attributes["Name"].Value));
                }

                CacheDependency cacheDependency = new CacheDependency(configPath);
                HttpContext.Current.Cache.Insert("CityList_" + provinceId, cityList, cacheDependency);
                return cityList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
